// pages/my/center/center.js
import dayjs from 'dayjs';
import uploadFile from '../../../utils/ossutil/uploadFile';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showNickName: false,
    showStanding: false,
    showInfo: false,
    list: [{
      leftIcon: "",
      rightIcon: "right",
      label: "绑定号码",
      value: "未实名",
      type: "text",
      right: true,
    }, {
      leftIcon: "",
      rightIcon: "right",
      label: "实名信息",
      value: "未实名",
      type: "text",
      right: true,
    }],
    userInfo: null,
    tempNickName: "",
    loading: true,
    identityList: [],
    selectUserIdentit: '',
  },
  setUserIdentit(e) {
    let {
      key,
      value
    } = getApp().utils.getParameter(e);
    this.setData({
      [key]: value,
    });
    if (value != undefined) {
      this.switchUserIdentity(value);
    }
  },
  async switchUserIdentity(identityId) {
    let res = await getApp().http.switchUserIdentity({
      identityId
    });
    if (res.errorCode === 200) {
      this.getSelectUserIdentity();
      this.setData({
        showStanding: false,
      })
      wx.showToast({
        title: '切换成功',
        icon: "none"
      })
    } else {
      wx.showToast({
        title: '切换失败',
        icon: "none"

      })
    }
  },
  goAuth() {
    wx.navigateTo({ 
      url: '/pages/my/authentication/authentication',
    })
  },
  /**
   * 获取身份列表
   */
  async getSelectUserIdentity() { 
    let data = {};
    let res = await getApp().http.selectUserIdentity(data);
    this.getCurrentUser();
    if (res.errorCode === 200) {
      let selectUserIdentit = "";
      const list = [];
      res.data.forEach(item => {
        if (item.identitySwitch) {
          selectUserIdentit = item.identityId
        }
        if (item.identityStatusValue === 1) {
          list.push(item);
        }
      })
      this.setData({
        selectUserIdentit: selectUserIdentit,
        identityList: list,
      })
    }
  },
  /**
   * 设置键盘高度
   * 
   */
  setKeyboardheight(e) {
    console.log(e);
  },
  /**
   * 获取
   */
  opneKey(e) {
    var {
      key
    } = getApp().utils.getParameter(e);
    this.setData({
      [key]: !this.data[key]
    })
  },
  /**
   * 设置昵称 
   */
  setNickname(e) {
    if (!e.detail.pass) {
      this.setData({
        "list[1].value": "",
      })
    } else {

    }
  },
  inputNickname(e) {
    this.setData({
      tempNickName: e.detail.value
    })
  },
  /**
   * 上传头像信息
   * 
   */
  async uploadFileAliyun(objType, objTypeStr, file) {
    let fileImageType = getApp().utils.getFileSuffix(file.url, ".");
    let timeFiles = dayjs(new Date()).format("YYYY/MM/DD");
    let fileName = `${objTypeStr}/${timeFiles}/${new Date().getTime()}.${fileImageType}`
    await uploadFile.uploadFile({
      fileName: fileName,
      filePath: file.url,
      successc: async (res) => {
        await getApp().http.updateUserHeadImageByAttachment({
          "filePath": fileName,
          "objType": objType,
          "fileSuffix": fileImageType
        });
        await getApp().http.updateUserInfo();
        wx.showToast({
          title: '更新成功',
        })
      }
    })
  },
  /**
   * 更新昵称
   */
  async updateUserNickName() {
    let formData = {
      "userNickName": this.data.tempNickName,
    }
    let res = await getApp().http.updateUserNickName(formData);
    if (res.errorCode === 200) {
      this.setData({
        "userInfo.userNickName": formData.userNickName,
        'showInfo': false
      });
      getApp().http.updateUserInfo();
      wx.showToast({
        title: '更新成功',
        icon: "none"
      })
    } else {
      wx.showToast({
        title: '更新失败',
        icon: "none"
      })
    }
  },
  /**
   *  选择头像 只有通过腾讯安全检测后才能触发该事件，否则不会触发
   */
  chooseavatar(e) {
    this.setData({
      "userInfo.userHeadImgUrl": e.detail.avatarUrl,
    })
    this.uploadFileAliyun(6, 'wechatUserHeadImage', {
      url: e.detail.avatarUrl
    });
  },
  /**
   * 关闭弹出层
   */
  onClose() {
    this.setData({
      showNickName: false,
      showStanding: false,
      showInfo: false
    })
  },
  /**
   * 获取线上用户信息 
   */
  async getCurrentUser() {
    let res = await getApp().http.currentUser();
    try {
      if (res.errorCode === 200) {
        console.log('用户信息', res.data);
        this.setData({
          loading: false,
          userInfo: res.data,
          'list[0].value': res.data.userMobile,
        });
      } else {
        console.log(res);
      }
    } catch (err) {
      console.log(err);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /** 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCurrentUser();
    this.getSelectUserIdentity();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})